/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-dealer
*
* @File name : MaintainController.java
*
* @Author : Administrator
*
* @Date : 2017年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月5日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.dealer.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.dealer.constant.DealerConstant;
import com.yonyou.gmmc.service.dealer.model.MaintainInfoList;
import com.yonyou.gmmc.service.dealer.model.TtReservation;
import com.yonyou.gmmc.service.dealer.service.MaintainService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* @author Administrator
* 企业号养修预约API
* @date 2017年12月5日
*/
@RestController
@RequestMapping(value = DealerConstant.BASE_PATH + "/" + DealerConstant.VERSION)
@Api("企业号养修预约API")
public class MaintainController {

    private Logger logger = Logger.getLogger(MaintainController.class);
    
    @Autowired
    private MaintainService maintainService;
    
    @SuppressWarnings("static-access")
    @RequestMapping(value = "/maintainRecords",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="获取该员工所在店的养修预约记录",notes="获取该员工所在店的养修预约记录")
    public Response<PageInfo<TtReservation>> maintainRecords(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                             @RequestParam(required = false, defaultValue = "10") String pageSize,
                                                             @RequestParam(required = false, defaultValue = "3") String flag,//1.今日养修;2.待确认;3.全部
                                                             @RequestParam(required = false, defaultValue = "") String submitDate,//1.今天;2.一周内;3开始日期-结束日期
                                                             @RequestParam(required = false, defaultValue = "") String orderDate,//1.一周内;2.一月内;3.开始日期-截止日期
                                                             @RequestParam(required = false, defaultValue = "") String orderStatus,//1.待确认;2.已确认;3.已到店;4.已取消;5.超时未处理;6.待受理
                                                             @RequestParam(required = false, defaultValue = "") String custTel,//客户手机
                                                             @RequestParam("phone") String phone) throws Exception{
        try{
            Map<String,Object> query = new HashMap<String,Object>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            query.put("pageNum", pageNum);
            query.put("pageSize", pageSize);
            query.put("flag", flag);
            if(!CheckUtil.NullOrEmpty(submitDate)){
                String submitStartDate = "";
                String submitEndDate = "";
                if(submitDate.equals("1")){
                    Date date=new Date();
                    submitStartDate = sdf.format(date)+" 00:00:00";
                    submitEndDate = sdf.format(date)+" 23:59:59";
                }else if(submitDate.equals("2")){
                    Date date=new Date();
                    Calendar calendar = new GregorianCalendar();  
                    calendar.setTime(date);  
                    calendar.add(calendar.DATE,-7);
                    date=calendar.getTime(); 
                    submitStartDate = sdf.format(date)+" 00:00:00";
                    submitEndDate = sdf.format(new Date())+" 23:59:59";
                }else{
                    String a[] = submitDate.split("-");
                    submitStartDate = a[0]+" 00:00:00";
                    submitEndDate = a[1]+" 23:59:59";
                }
                query.put("submitStartDate", submitStartDate);
                query.put("submitEndDate", submitEndDate);
            }
            if(!CheckUtil.NullOrEmpty(orderDate)){
                String orderStartDate = "";
                String orderEndDate = "";
                if(orderDate.equals("1")){
                    Date date=new Date();
                    Calendar calendar = new GregorianCalendar();  
                    calendar.setTime(date);  
                    calendar.add(calendar.DATE,-7);
                    date=calendar.getTime(); 
                    orderStartDate = sdf.format(date)+" 00:00:00";
                    orderEndDate = sdf.format(new Date())+" 23:59:59";
                }else if(orderDate.equals("2")){
                    Date date=new Date();
                    Calendar calendar = new GregorianCalendar();  
                    calendar.setTime(date);  
                    calendar.add(calendar.DATE,-30);
                    date=calendar.getTime(); 
                    orderStartDate = sdf.format(date)+" 00:00:00";
                    orderEndDate = sdf.format(new Date())+" 23:59:59";
                }else{
                    String a[] = orderDate.split("-");
                    orderStartDate = a[0]+" 00:00:00";
                    orderEndDate = a[1]+" 23:59:59";
                }
                query.put("orderStartDate", orderStartDate);
                query.put("orderEndDate", orderEndDate);
            }
            if(!CheckUtil.NullOrEmpty(orderStatus)){
                query.put("orderStatus", orderStatus);
            }
            query.put("phone", phone);
            PageInfo<TtReservation> resultList = maintainService.maintainRecords(query);
            return new Response<PageInfo<TtReservation>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultList, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    @RequestMapping(value = "/maintainInfo",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="获取该养修预约详情",notes="获取该养修预约详情")
    public Response<MaintainInfoList> maintainInfo(@RequestParam(required = false, defaultValue = "1") String flag,//1.不查询养修跟进记录;2.查询养修跟进记录
                                                   @RequestParam("phone") String phone,
                                                   @RequestParam("resnId") String resnId) throws Exception{
        MaintainInfoList result = new MaintainInfoList();
        try{
            result = maintainService.maintainInfo(flag,phone,resnId);
            return new Response<MaintainInfoList>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/affirmMaintain",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="确认预约",notes="确认预约")
    public Response<Map<String, Object>> affirmMaintain(@RequestBody @Valid TtReservation ttReservation,
    												    @RequestParam("phone") String phone )throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
        	resultMap = maintainService.affirmMaintain(ttReservation,phone);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/recordMaintain",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="添加跟进记录",notes="添加跟进记录")
    public Response<Map<String, Object>> recordMaintain(@RequestBody @Valid TtReservation ttReservation,
    												    @RequestParam("phone") String phone )throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
        	resultMap = maintainService.recordMaintain(ttReservation,phone);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/arriveMaintain",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="确认到店",notes="确认到店")
    public Response<Map<String, Object>> arriveMaintain(@RequestBody @Valid TtReservation ttReservation,
    												    @RequestParam("phone") String phone )throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
        	resultMap = maintainService.arriveMaintain(ttReservation,phone);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/cancleMaintain",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="取消预约",notes="取消预约")
    public Response<Map<String, Object>> cancleMaintain(@RequestBody @Valid TtReservation ttReservation,
    												    @RequestParam("phone") String phone )throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
        	resultMap = maintainService.cancleMaintain(ttReservation,phone);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/disposeMaintain",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value="抢先处理",notes="抢先处理")
    public Response<Map<String, Object>> disposeMaintain(@RequestBody @Valid TtReservation ttReservation,
    												    @RequestParam("phone") String phone )throws Exception{
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
        	resultMap = maintainService.disposeMaintain(ttReservation,phone);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
}
